package cn.kgc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.UserContextHolder;
import cn.kgc.constant.WmsPurchaseStatus;
import cn.kgc.dto.add.WmsPurchaseAddDTO;
import cn.kgc.dto.add.WmsPurchaseDetailAddDTO;
import cn.kgc.entity.WmsPurchaseDetail;
import cn.kgc.entity.WmsWareSku;
import cn.kgc.mapper.WmsPurchaseDetailMapper;
import cn.kgc.mapper.WmsWareSkuMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.mapper.WmsPurchaseMapper;
import cn.kgc.entity.WmsPurchase;
import cn.kgc.service.WmsPurchaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * 采购信息(WmsPurchase)表服务实现类
 *
 * @author 石浩杰
 * @since 2022-12-07 11:05:27
 */
@Service("wmsPurchaseService")
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseMapper, WmsPurchase> implements WmsPurchaseService {

    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Resource
    private WmsPurchaseDetailMapper wmsPurchaseDetailMapper;

    @Resource
    private WmsWareSkuMapper wmsWareSkuMapper;

    @Transactional
    public boolean saveWmsPurchase(WmsPurchaseAddDTO purchaseAddDTO) {
        //1 新建采购单
        WmsPurchase wmsPurchase = new WmsPurchase();
        BeanUtils.copyProperties(purchaseAddDTO, wmsPurchase);
        //采购单状态新建状态
        wmsPurchase.setStatus(WmsPurchaseStatus.NEW.getStatus());
        wmsPurchaseMapper.insert(wmsPurchase);
        //2 新建采购单详情
        List<WmsPurchaseDetailAddDTO> purchaseDetailAddDTOList = purchaseAddDTO.getWmsPurchaseDetailAddDTOList();
        //采购单总价
        BigDecimal totalPrice=new BigDecimal(0);
        for (WmsPurchaseDetailAddDTO purchaseDetailAddDTO : purchaseDetailAddDTOList) {
            WmsPurchaseDetail wmsPurchaseDetail = new WmsPurchaseDetail();
            BeanUtils.copyProperties(purchaseDetailAddDTO, wmsPurchaseDetail);
            wmsPurchaseDetail.setPurchaseId(wmsPurchase.getId());
            //每个订单详情对应的价格
            BigDecimal nums=new BigDecimal(purchaseDetailAddDTO.getSkuNum());
            BigDecimal tempPrice=purchaseDetailAddDTO.getSkuPrice().multiply(nums);
            totalPrice=totalPrice.add(tempPrice);
            wmsPurchaseDetailMapper.insert(wmsPurchaseDetail);
        }
        //采购单的总价
        wmsPurchase.setAmount(totalPrice);
        wmsPurchaseMapper.updateById(wmsPurchase);//更新总价
        return true;
    }

    @Transactional
    public boolean allocationWmsPurchase(Long id, Long assigneeId, String assigneeName, String phone) {
        WmsPurchase wmsPurchase=wmsPurchaseMapper.selectById(id);
        if (wmsPurchase==null){
            throw new HttpException(40001,500);
        }
        wmsPurchase.setStatus(WmsPurchaseStatus.ALLOCATION.getStatus());
        wmsPurchase.setAssigneeId(assigneeId);
        wmsPurchase.setAssigneeName(assigneeName);
        wmsPurchase.setPhone(phone);
        return wmsPurchaseMapper.updateById(wmsPurchase)>0;
    }

    @Override
    public boolean acceptWmsPurchase(long id) {
        //获得登录用户的id
        //String userId=UserContextHolder.getInstance().getUserId();
        String userId="1";
        WmsPurchase wmsPurchase=wmsPurchaseMapper.selectById(id);
        if (wmsPurchase==null){
            throw new HttpException(40003,500);
        }
        //1如果采购单状态不是已经分配，此时就不能够接收采购单
        if (wmsPurchase.getStatus()!=WmsPurchaseStatus.ALLOCATION.getStatus()){
            throw new HttpException(40004,500);
        }
        String assigneeId=wmsPurchase.getAssigneeId()+"";
        //2 接收采购的人和分配的人不是同一个人，不能够接收采购单
        if (!userId.equals(assigneeId)){
            throw new HttpException(40005,500);
        }
        //3更改状态(正在采购中)
        wmsPurchase.setStatus(WmsPurchaseStatus.SHOPPING.getStatus());
        return wmsPurchaseMapper.updateById(wmsPurchase)>0;
    }


    @Transactional
    public boolean finishWmsPurchase(Long id) {
        //获得登录用户的id
        //String userId=UserContextHolder.getInstance().getUserId();
        String userId="1";
        WmsPurchase wmsPurchase=wmsPurchaseMapper.selectById(id);
        if (wmsPurchase==null){
            throw new HttpException(40003,500);
        }
        //1如果采购单状态不是采购中，此时就不能够完成采购单
        if (wmsPurchase.getStatus()!=WmsPurchaseStatus.SHOPPING.getStatus()){
            throw new HttpException(40006,500);
        }
        String assigneeId=wmsPurchase.getAssigneeId()+"";
        //2 接收采购的人和分配的人不是同一个人，不能够进行完成操作
        if (!userId.equals(assigneeId)){
            throw new HttpException(40007,500);
        }
        //3 修改采购单状态
        wmsPurchase.setStatus(WmsPurchaseStatus.FINISH.getStatus());
        wmsPurchaseMapper.updateById(wmsPurchase);
        //4 商品入库(查询商品的用户信息)
        long wareId=wmsPurchase.getWareId();
        LambdaQueryWrapper<WmsPurchaseDetail> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmsPurchaseDetail::getPurchaseId,id);
        List<WmsPurchaseDetail> wmsPurchaseDetailList=wmsPurchaseDetailMapper.selectList(lambdaQueryWrapper);
        for (WmsPurchaseDetail wmsPurchaseDetail : wmsPurchaseDetailList) {
            //入库的商品原本不存在
            long skuId=wmsPurchaseDetail.getSkuId();
            int skuNum=wmsPurchaseDetail.getSkuNum();
            LambdaQueryWrapper<WmsWareSku> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(WmsWareSku::getSkuId,skuId);
            lambdaQueryWrapper1.eq(WmsWareSku::getWareId,wareId);
            int count=wmsWareSkuMapper.selectCount(lambdaQueryWrapper1);
            if (count==0){
                WmsWareSku wmsWareSku=new WmsWareSku();
                wmsWareSku.setSkuId(skuId);
                wmsWareSku.setWareId(wareId);
                wmsWareSku.setStock(skuNum);
                wmsWareSku.setSkuName(wmsPurchaseDetail.getSkuName());
                wmsWareSkuMapper.insert(wmsWareSku);
            }else {
                //入库的商品原来在仓库中，进行更新
                WmsWareSku wmsWareSku=wmsWareSkuMapper.selectOne(lambdaQueryWrapper1);
                wmsWareSku.setStock(wmsWareSku.getStock()+skuNum);
                wmsWareSkuMapper.updateById(wmsWareSku);
            }

        }
        return true;
    }
}
